<?php

/**
 * This is the model class for table "Partner".
 */

require_once dirname(__FILE__) . '/base/PartnerBase.php';
class Partner extends PartnerBase
{
    public $name;
    const STATUS_ACTIVE = 1, STATUS_DEACTIVE = 0;
    public $dateCreated = array('from'=>'','to'=>'');

    public function getStatusOptions()
    {
        return array(
            self::STATUS_ACTIVE => 'Active',
            self::STATUS_DEACTIVE => 'Inactive',
        );
    }

    public function getStatusText()
    {
        $options = $this->getStatusOptions();
        return isset($options[$this->status]) ? $options[$this->status] : Yii::t('app', 'Unknown {att}', array('{att}' => $this->status));
    }
    
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('status', 'numerical', 'integerOnly' => true),
            array('photo, website', 'length', 'max' => 255),
            array('create_time, update_time', 'safe'),
            array('website', 'url'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('name, status, dateCreated, website', 'safe', 'on' => 'search'),
        );
    }
    
    public function behaviors()
    {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_time',
                'updateAttribute' => 'update_time',
            )
        );
    }
    
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'localePartner' => array(self::HAS_ONE, 'LanguagePartner', 'partner_id'),
        );
    }
    
    public function search($selectedLocale)
    {
        $criteria = new CDbCriteria;
        $criteria->with = array(
            'localePartner' => array(
                'condition' => 'locale=:locale',
                'params' => array(':locale' => $selectedLocale)
            ),
        );
        $criteria->order = 't.create_time DESC, t.id DESC';
        
        $criteria->compare('website', $this->website, true);
        $criteria->compare('localePartner.name', $this->name, true);
        $criteria->compare('t.status', $this->status);
        
        // filter date_created - dateCreated
        if (is_array($this->dateCreated) && isset($this->dateCreated['from'], $this->dateCreated['to']) && (empty($this->dateCreated['from']) === false || empty($this->dateCreated['to']) === false)) {
            $from = $this->dateCreated['from'];
            $from = CDateTimeParser::parse($from, 'dd/MM/yyyy');
            if ($from === false) {
                $from = date('Y-m-d', strtotime('-1 years'));
            } else
                $from = date('Y-m-d', $from);
            $to = $this->dateCreated['to'];
            $to = CDateTimeParser::parse($to, 'dd/MM/yyyy');
            if ($to === false) {
                $to = date('Y-m-d', strtotime('+1 years'));
            } else
                $to = date('Y-m-d', $to);
            
            $criteria->addCondition("DATE(t.create_time) BETWEEN DATE('".$from."') AND DATE('".$to."')");
        }

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => Settings::BO_PAGE_SIZE
            ),
        ));
    }
}